//
//  EPMShoppingCarManger.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/5/4.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMShoppingCarManger.h"
#import "EPMCategoryModel.h"
typedef NS_ENUM(NSInteger, GoodChangeMethod){
    GoodChangeMethodDecrease = 1,
    GoodChangeMethodIncrease = 3
    
};
@interface EPMShoppingCarManger()
@property (strong,nonatomic) EPMGoodsModel* tempModel;
@end

@implementation EPMShoppingCarManger

+ (instancetype)sheardManger{
    static EPMShoppingCarManger* _instance;
    static dispatch_once_t token;
    dispatch_once( &token,^{
        _instance = [EPMShoppingCarManger new];
        _instance.isEmpty = YES;
        
    });
    
    return _instance;
}

- (instancetype)init{
    if(self = [super init]){
        [self setting];
    }
    return self;
}
#pragma mark - 设置参数变量 -

- (void)setting{
    
    _goodsList = [NSMutableArray array];
    [self addNotifications];
}
#pragma mark - 添加通知 -

- (void)addNotifications{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shopCarIncreaseGoodsNotification:) name:EPMSuperIncreaseGoodsNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shopCarIncreaseGoodsNotification:) name:EPMHomeIncreaseGoodsNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(marketDecreaseGoodNotification:) name:EPMMarketDecreaseGoodNotification object:nil];
}

#pragma mark - 通知事件处理 -

- (void)shopCarIncreaseGoodsNotification:(NSNotification*)noti{
    
    
    _totalGoodNum ++;
    self.isEmpty = (self.totalGoodNum == 0);
    EPMGoodsModel* model =
    noti.userInfo[KIncreaseGoodsModel];
    //更新good模型的count
    [self goodCountChanged:model changeMethod:GoodChangeMethodIncrease];
    if (self.goodsList.count == 0){
        
        [self.goodsList addObject:self.tempModel];
        
    }else{
    for (int i = 0 ; i < self.goodsList.count ; i ++){
        EPMGoodsModel* tempModel = self.goodsList[i];
        if (tempModel.id == self.tempModel.id){

            break;
        }else if (i == self.goodsList.count - 1){
          [self.goodsList addObject:self.tempModel];
            break;
        }
      }
    }
    
    //发送数量变化通知
    [[NSNotificationCenter defaultCenter] postNotificationName:EPMShopCarGoodsNumChanged object:nil userInfo:@{KShopCarGoodsNum:@(_totalGoodNum)}];
    //发送列表变化
    [[NSNotificationCenter defaultCenter] postNotificationName:EPMShopCarGoodListChangedNotification object:nil userInfo:@{KShopCarGoodList:self.goodsList}];
}

- (void)marketDecreaseGoodNotification:(NSNotification*)noti{
    _totalGoodNum --;
    self.isEmpty = (self.totalGoodNum == 0);
    EPMGoodsModel* model = noti.userInfo[KDecreaseGoodsModel];
    //更新good模型的count
    [self goodCountChanged:model changeMethod:GoodChangeMethodDecrease];
    for (EPMGoodsModel* tempModel in self.goodsList){
        if (tempModel.id == self.tempModel.id){
            if (tempModel.count == 0){
                [self.goodsList removeObject:self.tempModel];
                break;
            }
            break;
        }
    }
    
    //发送数据变化通知
    [[NSNotificationCenter defaultCenter] postNotificationName:EPMShopCarGoodsNumChanged object:nil userInfo:@{KShopCarGoodsNum:@(_totalGoodNum)}];
    //发送列表变化
     [[NSNotificationCenter defaultCenter] postNotificationName:EPMShopCarGoodListChangedNotification object:nil userInfo:@{KShopCarGoodList:self.goodsList}];
    
}

- (void)goodCountChanged:(EPMGoodsModel*)model changeMethod:(GoodChangeMethod)method{
    
    
    NSInteger pid = [model.category_id integerValue];
    
    
    for ( EPMCategoryModel* catergory in _MarketManger.catergoryList ){
        if ([catergory.id integerValue] == pid ){
            NSArray* goodList = catergory.goodList;
            for (EPMGoodsModel* good in goodList){
                if (good.id  == model.id){
                    good.count += (method -2);
                    self.tempModel = good;
                    break;
                }
                
            }
        }
    }
}

- (BOOL)isEmpty{
    return self.goodsList.count == 0;
}

@end
